package v1

import (
	"chat/internal/common/request"
	"chat/internal/common/response"
	"chat/internal/service"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

func GetFriendIdList(c *gin.Context) {
	idStr, _ := c.GetQuery("uid")
	id, err := strconv.Atoi(idStr)
	if err != nil {
		return
	}

	list, err := service.GetFriendIdList(id)
	if err == nil {
		c.JSON(http.StatusOK, response.SuccessMsg(list))
		return
	}

	c.JSON(http.StatusOK, response.FailMsg("GetFriendIdList failed"))
	return
}

func GetUserFriendByUId(c *gin.Context) {
	idStr, _ := c.GetQuery("uid")
	id, err := strconv.Atoi(idStr)
	if err != nil {
		return
	}

	list, err := service.GetUserFriendList(id)
	if err == nil {
		c.JSON(http.StatusOK, response.SuccessMsg(list))
		return
	}

	c.JSON(http.StatusOK, response.FailMsg("GetFriendIdList failed"))
	return
}

func CreateRelation(c *gin.Context) {
	var req request.CreateRelationRequest
	c.ShouldBindJSON(&req)

	err := service.CreateRelation(req.UserId, req.FriendId)
	if err == nil {
		c.JSON(http.StatusOK, response.SuccessMsg(nil))
		return
	}

	c.JSON(http.StatusOK, response.FailMsg("CreateRelation failed"))
	return
}
